import { Component } from '@angular/core';

import axios from "axios";

import { CommonService } from "../services/common.service";

import { ReuseModule } from "../modules/reuse/reuse.module";

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {

  public list: any[] = [];
  public pageIndex = 1;
  public api_Base: String = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=';
  public initData: boolean = false;

  constructor(public commonService: CommonService) {
    console.log(commonService);
    // console.log('aaaa');
    //this.getData(null);
    //this.getDataVer2(null);
    this.initData = false;
    this.getDataVer3(null);
  }

  getDataVer3(event) {
    let url = this.api_Base + this.pageIndex.toString();
    this.commonService.ajaxGet(url)
      .then((data: any) => {
        //console.log(data);

        this.list = this.list.concat(data.result);
        //console.log(this.list);
        if (data.result.length < 20) {
          event ? event.target.enabled = false : '';
        }

        this.initData = true;

        ++this.pageIndex;
        event ? event.target.complete() : '';
      })
      .catch((error) => {
        console.log(error);
      });
  }

  /**
   * 
   *api_Base: String = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=';
   */
  getData(event) {
    console.log(event);
    let temp = [];
    let begin = 20 * this.pageIndex;
    let end = begin + 20;
    for (let index = begin; index < end; index++) {
      temp.push(index);
    }
    ++this.pageIndex;
    this.list = this.list.concat(temp);

    event ? event.target.complete() : '';
  }

  getDataVer2(event) {
    let url = this.api_Base + this.pageIndex.toString();
    axios.get(url)
      .then((resp) => {
        //console.log(resp.data);

        //this.list = resp.data.result;
        this.list = this.list.concat(resp.data.result);
        //console.log(this.list);
        if (resp.data.result.length < 20) {
          event.target.enabled = false;
        }

        ++this.pageIndex;
        event ? event.target.complete() : '';

      })
      .catch((error) => {
        console.log(error);
      });
  }

}
